<?php


namespace app\index\exception;

use think\exception\Handle;
use think\Response;
use Throwable;
class Http extends Handle
{
    public $httpCode = 500;
    /**
     * demo控制器下的异常处理
     * @param \think\Request $request
     * @param Throwable $e
     * @return Response
     */
    public function render($request, Throwable $e): Response
    {
        if($e instanceof \think\Exception){
            return show($e->getCode(),$e->getMessage());
        }
        if ($e instanceof \think\exception\HttpResponseException){
            return parent::render($request,$e);
        }
        // 添加自定义异常处理机制
        // if是兼容在控制器中throw抛出的异常
        if (method_exists($e,"getStatusCode")){
            $httpCode = $e->getStatusCode();
        }else{
            $httpCode = $this->httpCode;

        }
        return show(config("status.error"),$e->getMessage(),"",$httpCode);

    }
}